#include<stdio.h>
#include<stdlib.h>
#include<string.h>

typedef struct filedescripter{
	//int fdno;	
	char filename[20];
	char filepath[50];
}fd;


typedef struct node{
	fd  *file;
	struct node *leftchild;
	struct node *rightsibling;
}node; 


char filename[20],filepath[50];
//int fdno;
node *rootbst;

int searchbst(char *filepath,node *rootbst);
node* insertbst(node * rootbst);
node* addfilebst(char *filename,char *filepath/*,int fdno*/);
void displaybst(node * rootbst);
/*void main(){
	int a,b;
	
	while(1){
	printf("Enter your choice \n 1: Add file \n 2: Search file \n 3:display");
	scanf("%d",&a);
	
	switch(a){
		case 1:
			do{
				printf("Enter the filename or 0 to abort\n");
				scanf("%s", filename);
				if(strcmp(filename,"0"))
				{				
				printf("Enter the filepath\n");
				scanf("%s", filepath);
				//printf("Enter the fdno.\n");
				//scanf("%d",& fdno);
				
				rootbst = insert(rootbst);
				}
				else
					break;	
			}while(1);	
			break;
		case 2:
			printf("Enter the filepath to search\n");
			scanf("%s", filepath);
			b=search(filepath,rootbst);
		case 3:
			display(rootbst);
			
         	break;
		default:
			break;
		}
	}
	
}

*/
node* addfilebst(char filename[],char filepath[]/*, int fdno*/){
	node *temp;
	fd *tempfile;
	

	temp=(node*)malloc(sizeof(node));     
	temp->rightsibling=NULL;
	temp->leftchild=NULL;
	tempfile = (fd*)malloc(sizeof(fd));
	strcpy(tempfile->filename,filename);
	strcpy(tempfile->filepath,filepath);
	//tempfile->fdno=fdno;
	temp->file= tempfile;
	return temp;


}

node* insertbst(node* temp){
	if(rootbst==NULL){
		rootbst= addfile(filename,filepath/*,fdno*/);
		return rootbst;
	}	
	if(rootbst->leftchild == NULL && strcmp(rootbst->file->filename,filename)<0){
		rootbst->leftchild = addfile(filename,filepath/*,fdno*/);
		return rootbst;
	}
	if(rootbst->rightsibling==NULL && strcmp(rootbst->file->filename,filename)>0){
		rootbst->rightsibling = addfile(filename,filepath/*,fdno*/);
		return rootbst;
	}


	if(temp->leftchild == NULL && strcmp(temp->file->filename,filename)<0){
		temp->leftchild = addfile(filename,filepath/*,fdno*/);
		return rootbst;
	}		
	else if	(strcmp(temp->file->filename,filename)<0){
		temp=temp->leftchild; 
		rootbst=insert(temp);
		return rootbst;
	}


	if(temp->rightsibling==NULL && strcmp(temp->file->filename,filename)>0){
		temp->rightsibling = addfile(filename,filepath/*,fdno*/);
		return rootbst; 
	}
	else if	(strcmp(temp->file->filename,filename)>0){
		temp=temp->rightsibling; 
		rootbst=insert(temp);
		return rootbst;
	}
		
}


int searchbst(char filepath[],node *temp){	
	if(temp != NULL){
	search(filepath,temp->leftchild);
	if(!(strcmp(filepath,temp->file->filepath))){
		printf("%s\n%s\n",temp->file->filepath,temp->file->filename/*,temp->file->fdno*/);
		return 0;
	}
	search(filepath,temp->rightsibling);
	}
}

void displaybst(node *info)
{
	
	
	if(info!=NULL)
	{
           displaybst(info->leftchild);
	   printf("  %s  ",info->file->filename);
           displaybst(info->rightsibling);
        } 
}







